package com.fyh.hiring.company.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fyh.hiring.company.dao.PositionDao;
import com.fyh.hiring.company.entity.PositionEntity;

/**
 * 
 * @author 付元辉.
 *
 * @since2016年2月27日
 */
public class PositionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PositionDao positionDao = new PositionDao();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 设置post请求的编码
		request.setCharacterEncoding("UTF-8");

		// 因为一个servlet要做多件事情，所以根据命令参数，决定调用那个方法
		String command = request.getParameter("command");
		if ("showPosition".equals(command)) {
			// 调用职位展示的方法
			try {
				showPosition(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	/**
	 * 职位展示的方法
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @throws SQLExceptions
	 */
	private void showPosition(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		// 1.获取参数
		// 2.调用业务逻辑
		List<PositionEntity> positionList = positionDao.showPosition();
		// 3.转向,由于要携带参数，最好使用这个
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("view/company/position-list.jsp?").forward(
				request, response);

	}

}
